package com.car.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.account.dto.storesDimissionDto.SelectDimissionByStateDto;
import com.car.account.entity.Stores;
import com.car.account.entity.StoresDimission;
import com.car.account.exception.AccountException;
import com.car.account.mapper.StoresDimissionMapper;
import com.car.account.param.storesDimissionParam.OverruleDimissionParam;
import com.car.account.param.storesDimissionParam.SelectDimissionParam;
import com.car.account.service.StoresDimissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.account.service.StoresService;
import com.car.account.util.PageDtoUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class StoresDimissionServiceImpl extends ServiceImpl<StoresDimissionMapper, StoresDimission> implements StoresDimissionService {

    @Resource
    private StoresService storesService;

    @Override
    @Transactional
    public void clerkDimissionApplication(Integer clerkId, String dimissionInfo) throws Exception {
        //判断是否存在门店账号
        Stores stores = storesService.getById(clerkId);
        if (stores == null || stores.getClerkState() == 2) throw new AccountException(6001,"店员账号不存在");
        //判断门店账号状态
        if (stores.getClerkState() == 1) throw new AccountException(6002,"店员账号已被禁用");
        //查询是否已经存在离职申请
        QueryWrapper<StoresDimission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clerk_id", clerkId).eq("dimission_state", 0);
        StoresDimission dimission = getOne(queryWrapper);
        if (dimission != null) throw new AccountException(6003,"已有待审核的离职申请");
        //生成离职申请
        StoresDimission storesDimission = new StoresDimission();
        storesDimission.setDimissionInfo(dimissionInfo);
        storesDimission.setClerkId(stores.getClerkId());
        storesDimission.setClerkName(stores.getClerkName());
        storesDimission.setStores(stores.getStoresId());
        storesDimission.setDimissionState(0);
        save(storesDimission);
    }

    @Override
    public Page<SelectDimissionByStateDto> selectDimissionByState(SelectDimissionParam param) throws Exception {
        //判断是否存在门店账号
        Stores stores = storesService.getById(param.getClerkId());
        if (stores == null || stores.getClerkState() == 2) throw new AccountException(6001,"店长账号不存在");
        //判断门店账号状态
        if (stores.getClerkState() == 1) throw new AccountException(6002,"店长账号已被禁用");
        //查询符合条件的申请
        QueryWrapper<StoresDimission> queryWrapper = new QueryWrapper<StoresDimission>()
                .eq("stores", stores.getStoresId());
        if (param.getDimissionState() != null) queryWrapper.eq("dimission_state", param.getDimissionState());
        Page<StoresDimission> page = new Page<>(param.getCurrent()==null?1:param.getCurrent(), param.getSize()==null?6:param.getSize());
        Page<StoresDimission> storesDimissionPage = page(page, queryWrapper);
        Page<SelectDimissionByStateDto> selectDimissionByStateDtoPage = PageDtoUtil.convertPage(storesDimissionPage, SelectDimissionByStateDto.class);
        return selectDimissionByStateDtoPage;
    }

    @Override
    @Transactional
    public void passDimissionById(Integer clerkId, Integer dimissionId) throws Exception {
        //判断是否存在门店账号
        Stores stores = storesService.getById(clerkId);
        if (stores == null || stores.getClerkState() == 2) throw new AccountException(6001,"店长账号不存在");
        //判断门店账号状态
        if (stores.getClerkState() == 1) throw new AccountException(6002,"店长账号已被禁用");
        //判断申请状态
        StoresDimission storesDimission = getById(dimissionId);
        if (storesDimission == null) throw new AccountException(6003,"申请不存在");
        if (storesDimission.getDimissionState() != 0) throw new AccountException(6003,"不能重复处理");
        //修改申请状态
        storesDimission.setDimissionState(1);
        updateById(storesDimission);
        //查询申请员工
        Stores clerk = storesService.getById(storesDimission.getClerkId());
        //修改员工状态
        clerk.setClerkState(2);
        storesService.updateById(clerk);
    }

    @Override
    @Transactional
    public void overruleDimissionById(OverruleDimissionParam param) throws Exception {
        //判断是否存在门店账号
        Stores stores = storesService.getById(param.getClerkId());
        if (stores == null || stores.getClerkState() == 2) throw new AccountException(6001,"店长账号不存在");
        //判断门店账号状态
        if (stores.getClerkState() == 1) throw new AccountException(6002,"店长账号已被禁用");
        //判断申请状态
        StoresDimission storesDimission = getById(param.getDimissionId());
        if (storesDimission == null) throw new AccountException(6003,"申请不存在");
        if (storesDimission.getDimissionState() != 0) throw new AccountException(6003,"不能重复处理");
        //修改申请状态
        storesDimission.setDimissionState(2);
        storesDimission.setOverrule(param.getOverrule());
        updateById(storesDimission);
    }
}
